/**
 * Title:        代理数据同步
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;



public class RequestSequenceCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("RequestSequenceCheck  start");

        List resultList = new ArrayList();
        String result = resultOk;
        if(recordSet.getDBType().equals("oracle")){

            //currentoperator
            int curoptCurVal = -1;
            int curoptMaxId = -1;
            recordSet.executeSql(" select t_wf_curopt_id.currval from dual ");
            if(recordSet.next()){
                curoptCurVal = recordSet.getInt(1);
            }
            
            recordSet.executeSql(" select max(id) from workflow_currentoperator ");
            if(recordSet.next()){
                curoptMaxId = recordSet.getInt(1);
            }
            if(curoptCurVal < curoptMaxId){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage","操作者表中最大的ID(" + curoptMaxId + ")大于对应的SEQ中的当前值(" + curoptCurVal + ")，会导致插入错误。");
                resultMap.put("buttonname","数据同步");
                resultMap.put("seqid","t_wf_curopt_id");
                resultList.add(resultMap);
            }

            //requestLog
            int requestLogCurVal = -1;
            int requestLogMaxId = -1;
            recordSet.executeSql(" select workflow_requestlog_id.currval from dual ");
            if(recordSet.next()){
                requestLogCurVal = recordSet.getInt(1);
            }
            
            recordSet.executeSql(" select max(logid) from workflow_requestlog ");
            if(recordSet.next()){
                requestLogMaxId = recordSet.getInt(1);
            }
            if(requestLogCurVal < requestLogMaxId){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage","流转意见表中最大的ID(" + requestLogMaxId + ")大于对应的SEQ中的当前值(" + requestLogCurVal + ")，会导致插入错误。");
                resultMap.put("buttonname","数据同步");
                resultMap.put("seqid","workflow_requestlog_id");
                resultList.add(resultMap);
            }
        }
        
        (new BaseBean()).writeLog("RequestSequenceCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();
        
        if(dataMap!= null && dataMap.get("divid")!= null){
            List resultList = new ArrayList();
            String result = "";
            String divid = (String)dataMap.get("divid");

            recordSet.executeUpdate("update workflow_agent set workflow_agent.agenttype = '0'  where workflow_agent.agenttype = '1' and not exists (select 1 from workflow_agentconditionset b where workflow_agent.agentid =  b.agentid and b.agenttype = '1')");
            
            recordSet.executeSql("select count(1) as cnt from workflow_agent a where a.agenttype = '1' and not exists (select 1 from workflow_agentconditionset b where a.agentid =  b.agentid and b.agenttype = '1')");
            
            if(recordSet.next()){
                int cnt = recordSet.getInt("cnt");
                if(cnt > 0){
                    result = resultNeedFix;
                    Map resultMap = new HashMap<String,String>();
                    resultMap.put("result",resultNeedFix);
                    resultMap.put("fixMessage","数据修复失败");
                    resultMap.put("divid",divid);
                    resultList.add(resultMap);
                }else{
                    result = resultOk;
                    Map resultMap = new HashMap<String,String>();
                    resultMap.put("result",resultOk);
                    resultMap.put("fixMessage","数据修复完成");
                    resultMap.put("divid",divid);
                    resultList.add(resultMap);
                }
            }
            return formatReturnJsonFix(result, resultList,this);
        }else{
            setRecordSet();

            recordSet.executeUpdate("update workflow_agent set workflow_agent.agenttype = '0'  where workflow_agent.agenttype = '1' and not exists (select 1 from workflow_agentconditionset b where workflow_agent.agentid =  b.agentid and b.agenttype = '1')");
            
            return checkData(dataMap);
        }
        
        
    }

    @Override
    public String getClassName() {
        return "检测SEQ当前值";
    }

    @Override
    public String getClassDesc() {
        return "检测流转意见表和操作者表，SEQ是否大于该表最大的id";
    }

}



